extends Node2D


@onready var enter_area = $EnterArea
@onready var player = %Player

@export var exit_point: Node2D
@export_enum("ui_up","ui_down","ui_left","ui_right") var enter_key : String = "ui_down"

var player_in = false
var color_fill = Color(0,0,0,255)
var color_clear = Color(0,0,0,0)
const PIPE = preload("res://asserts/sound/pipe.ogg")

func _ready():
	enter_area.body_entered.connect(check_player_enter)
	enter_area.body_exited.connect(check_player_exit)

func _process(_delta):
	if Input.is_action_pressed(enter_key) && player_in:
		%Player.z_index = 0
		playTweenAminate()
		%Sound.stream = PIPE
		%Sound.play()

func check_player_exit(body: Node2D):
	print(body.name + " body_exited!")
	player_in = false

func check_player_enter(body: Node2D):
	print(body.name + " body_entered!")
	player_in = true

func playTweenAminate():
	var tween = get_tree().create_tween()
	var target_posision = player.position
	match enter_key:
		"ui_up": 
			target_posision.y -= 24
		"ui_down":
			target_posision.y += 24
		"ui_left":
			target_posision.x -= 24
		"ui_right":
			target_posision.x += 24
	tween.tween_property(player, "position",target_posision, 0.5) # 移动到 (200, 200)
	tween.tween_callback(trigger_transport)

func trigger_transport():
	%Player.z_index = 2
	player.position = exit_point.position
